package com.kgc.xsConsumer.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.xs.pojo.SalesQuotes;
import com.kgc.xs.pojo.SalesQuotesDetail;
import com.kgc.xsConsumer.feign.SalesQuotesFeign;

import com.kgc.xsConsumer.feign.XSWorkFlowFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("xs")
public class SalesQuotesController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SalesQuotesFeign salesQuotesFeign;

    @Autowired
    private XSWorkFlowFeign xsWorkFlowFeign;

    @RequestMapping("/selectSaleQuotesByPage")
    public ResponseMsg selectSaleQuotesByPage(@RequestBody Map<String, Object> map) {
        logger.info("SalesQuotesController selectSaleQuotesByPage params {}", map.toString());
        ResponseMsg responseMsg=salesQuotesFeign.selectSalesQuotesByPage(map);
        logger.debug("SalesQuotesController selectSaleQuotesByPage return{}",responseMsg);
        return responseMsg;

    }

    @RequestMapping("/insertSalesQuotes")
    public  ResponseMsg insertSalesQuotes(@RequestBody Map<String, Object> map) {
        logger.info("SalesQuotesController insertSalesQuotes params {}", map.toString());
        ResponseMsg responseMsg=salesQuotesFeign.insertSalesQuotes(map);
        ObjectMapper mapper = new ObjectMapper();
        // 转换 SalesOrder 对象
        SalesQuotes salesQuote=mapper.convertValue(map.get("salesQuote"), SalesQuotes.class);

        Map<String,Object> variables = new HashMap<>();
        variables.put("code",salesQuote.getDoCode());//单据编号
        variables.put("type",salesQuote.getTypeId());//流程类型
        //创建一个流程实例
        xsWorkFlowFeign.startProcess(variables);
        logger.debug("SalesQuotesController insertSalesQuotes return{}",responseMsg);
        return responseMsg;
    }



    @RequestMapping("/insertSalesQuotesDetail")
    public ResponseMsg insertSalesQuotesDetail(@RequestBody List<SalesQuotesDetail> details) {
        logger.info("SalesQuotesController insertSalesQuotesDetail params {}", details);
        ResponseMsg responseMsg=new ResponseMsg();
        try {
            for (SalesQuotesDetail detail : details) {
                salesQuotesFeign.insertSalesQuotesDetail(detail);
            }
            responseMsg.setCode("200");
        }catch (Exception e){
            responseMsg.setCode("201");

         return responseMsg;
        }
        logger.debug("SalesQuotesController insertSalesQuotesDetail return{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectSalesQuotesDetail")
    public ResponseMsg selectSalesQuotesDetail(@RequestBody  Map<String, Object> map) {
        logger.info("SalesQuotesController selectSalesQuotesDetail params {}", map.toString());
        ResponseMsg responseMsg=salesQuotesFeign.selectSalesQuotesByPage(map);
        logger.debug("SalesQuotesController selectSalesQuotesDetail return{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/updateSalesQuotes")
    public  ResponseMsg updateSalesQuotes(@RequestBody Map<String, Object> map) {
        logger.info("SalesQuotesController updateSalesQuotes params {}", map.toString());
        ResponseMsg responseMsg=salesQuotesFeign.updateSalesQuotes(map);
        logger.debug("SalesQuotesController updateSalesQuotes return{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/updateSalesQuotesDetail")
    public ResponseMsg  updateSalesQuotesDetail(@RequestBody SalesQuotesDetail salesQuotesDetail) {
        logger.info("SalesQuotesController updateSalesQuotesDetail params {}", salesQuotesDetail);
        ResponseMsg responseMsg=salesQuotesFeign.updateSalesQuotesDetail(salesQuotesDetail);
        logger.debug("SalesQuotesController updateSalesQuotesDetail return{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectSalesQuotesByCode")
    public  ResponseMsg selectSalesQuotesByCode(String doCode) {
        logger.info("SalesQuotesController selectSalesQuotesByCode params {}", doCode);
        ResponseMsg responseMsg=salesQuotesFeign.selectSalesQuotesByCode(doCode);
        logger.debug("SalesQuotesController selectSalesQuotesByCode return{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectSalesQuotesDetailByCode")
    public  ResponseMsg insertSalesQuotesDetailByCode(String bjCode) {
        logger.info("SalesQuotesController insertSalesQuotesDetailByCode params {}", bjCode);
        ResponseMsg responseMsg=salesQuotesFeign.selectSalesQuotesDetailByCode(bjCode);
        logger.debug("SalesQuotesController insertSalesQuotesDetailByCode return{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/check")
    public ResponseMsg  check(@RequestBody Map<String, Object> map){
        logger.info("SalesQuotesController check params {}", map.toString());
        ResponseMsg responseMsg=new ResponseMsg();
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",(String) map.get("docCode"));//单据编号
        variables.put("checkManId",(Integer) map.get("checkManId"));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  xsWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        logger.debug("SalesQuotesController check return{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/selectSalesQuotesByCid")
    public  ResponseMsg selectSalesQuotesByCid(@RequestParam("cid") int cid) {
        logger.info("SalesQuotesController selectSalesQuotesByCid params {}", cid);
        ResponseMsg responseMsg=salesQuotesFeign.selectSalesQuotesByCid(cid);
        logger.debug("selectSalesQuotesByCid return{}",responseMsg);
        return responseMsg;
    }


    @RequestMapping("/deleteSalesQuotesByCode")
    public  ResponseMsg deleteSalesQuotesByCode(@RequestBody Map<String,Object> map) {
        logger.info("SalesQuotesController deleteSalesQuotesByCode params {}", map.toString());
        ResponseMsg responseMsg=salesQuotesFeign.deleteSalesQuotes(map);
        logger.debug("SalesQuotesController deleteSalesQuotesByCode return{}",responseMsg);
        return responseMsg;
    }

    @RequestMapping("/checkStatus")
    public  ResponseMsg checkStatus(@RequestBody Map<String, Object> map) {
        logger.info("SalesQuotesController checkStatus params {}", map.toString());
        ResponseMsg responseMsg=salesQuotesFeign.check(map);
        logger.debug("SalesQuotesController checkStatus return{}",responseMsg);
        return responseMsg;
    }


}
